<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/25 0025
 * Time: 9:42
 */

namespace app\storage\model;


use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Model;


class TransferModel extends Model
{
    protected $table = 'smt_transfer';
    protected $pk = 'uuid';


    public function Storage(){
        return $this->belongsTo('StorageModel','storage_uuid','uuid')->bind('name');
    }

    /***
     * @param $condition
     * @return mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws \think\Exception
     * 获取中转区的物料
     */
    public function getTransfer($condition){
        $where = $condition['where'];
        $pageIndex = $condition['pageIndex'];
        $pageSize = $condition['pageSize'];
        if (empty($where['name'])) {
            $hasWhere = [];
        } else {
            $hasWhere['name'] = $where['name'];
            unset($where['name']);
        }
        if(!empty($where['status'])){
            $where['transfer.status'] = $where['status'];
            unset($where['status']);
        }
        $ret['total'] = $this->alias('transfer')
            ->join(['smt_feed_station_detail' => 'detail'], 'transfer.feed_detail_uuid = detail.uuid')
            ->join(['iwh_storage' => 'storage'], 'storage.uuid = transfer.storage_uuid')
            ->where($where)
            ->count();

        $ret['row'] = $this->alias('transfer')
            ->where($where)
            ->order('transfer.time desc')
            ->join(['smt_feed_station_detail' => 'detail'], 'transfer.feed_detail_uuid = detail.uuid')
            ->join(['iwh_storage' => 'storage'], 'storage.uuid = transfer.storage_uuid')
            ->page($pageIndex,$pageSize)
            ->field('transfer.*,storage.name,storage.uuid,detail.feed_id,detail.machine_id,detail.seat_code')
            ->select();

        return $ret;

    }
}